package com.hqd.ch03.v23.config;

import com.hqd.ch03.v23.aop.framework.autoproxy.AspectJAwareAdvisorAutoProxyCreator;
import com.hqd.ch03.v23.factory.ConfigurableBeanFactory;
import com.hqd.ch03.v23.reader.xml.ParserContext;
import com.hqd.ch03.v23.registry.BeanDefinitionRegistry;
import org.w3c.dom.Element;

public abstract class AopNamespaceUtils {
    public static final String AUTO_PROXY_CREATOR_BEAN_NAME =
            "org.springframework.aop.config.internalAutoProxyCreator";
    public static final String PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class";

    private static final String EXPOSE_PROXY_ATTRIBUTE = "expose-proxy";

    public static void registerAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Element element) {
        BeanDefinition beanDefinition = registerAspectJAutoProxyCreatorIfNecessary(AspectJAwareAdvisorAutoProxyCreator.class, parserContext.getRegistry());
        useClassProxyingIfNecessary(parserContext.getRegistry(), element);
        registerComponentIfNecessary(beanDefinition, parserContext);
    }

    private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, Element sourceElement) {
        if (sourceElement != null) {
            //判断是否开启proxy-target-class，使用代理类
            boolean proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE));
            if (proxyTargetClass) {
                forceAutoProxyCreatorToUseClassProxying(registry);
            }
            // expose-proxy 配置只在使用基于接口的AOP代理时有效，是否可以获取到代理类
            boolean exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE));
            if (exposeProxy) {
                forceAutoProxyCreatorToExposeProxy(registry);
            }
        }
    }

    public static void forceAutoProxyCreatorToExposeProxy(BeanDefinitionRegistry registry) {
        if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
            BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
            definition.getPropertyValues().add("exposeProxy", "true");
        }
    }

    public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
        if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
            BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
            definition.getPropertyValues().add("proxyTargetClass", "true");
        }
    }

    private static void registerComponentIfNecessary(BeanDefinition beanDefinition, ParserContext parserContext) {
        if (beanDefinition != null) {
            parserContext.getRegistry().registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
        }
    }

    private static BeanDefinition registerAspectJAutoProxyCreatorIfNecessary(
            Class<?> cls, BeanDefinitionRegistry registry) {
        BeanDefinition beanDefinition = new BeanDefinition(cls);
        beanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
        registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
        return beanDefinition;
    }
}
